package com.scmt.hmzd.controller;

import java.util.Arrays;
import java.util.Date;

import com.scmt.hmzd.service.IHmOrderImgService;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.core.common.utils.SecurityUtil;
import com.scmt.hmzd.entity.HmOrderImg;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scmt.core.common.enums.LogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author
 **/
@RestController
@Api(tags = " 订单技师上传的图片数据接口")
@RequestMapping("/scmt/hmOrderImg")
public class HmOrderImgController {
    @Autowired
    private IHmOrderImgService hmOrderImgService;
    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 功能描述：新增订单技师上传的图片数据
     *
     * @param hmOrderImg 实体
     * @return 返回新增结果
     */
    @ApiOperation("新增订单技师上传的图片数据")
    @PostMapping("addHmOrderImg")
    public Result<Object> addHmOrderImg(@RequestBody HmOrderImg hmOrderImg) {
        try {
            hmOrderImg.setCreateTime(new Date());
            boolean res = hmOrderImgService.save(hmOrderImg);
            if (res) {
                return ResultUtil.data(res, "保存成功");
            } else {
                return ResultUtil.data(res, "保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：更新数据
     *
     * @param hmOrderImg 实体
     * @return 返回更新结果
     */
    @ApiOperation("更新订单技师上传的图片数据")
    @PostMapping("updateHmOrderImg")
    public Result<Object> updateHmOrderImg(@RequestBody HmOrderImg hmOrderImg) {
        if (hmOrderImg.getId() == null) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderImgService.updateById(hmOrderImg);
            if (res) {
                return ResultUtil.data(res, "修改成功");
            } else {
                return ResultUtil.data(res, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来删除数据
     *
     * @param ids 主键集合
     * @return 返回删除结果
     */
    @ApiOperation("根据主键来删除订单技师上传的图片数据")
    @PostMapping("deleteHmOrderImg")
    public Result<Object> deleteHmOrderImg(@RequestParam String[] ids) {
        if (ids == null || ids.length == 0) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderImgService.removeByIds(Arrays.asList(ids));
            if (res) {
                return ResultUtil.data(res, "删除成功");
            } else {
                return ResultUtil.data(res, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来获取数据
     *
     * @param id 主键
     * @return 返回获取结果
     */
    @ApiOperation("根据主键来获取订单技师上传的图片数据")
    @GetMapping("getHmOrderImg")
    public Result<Object> getHmOrderImg(@RequestParam(name = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            HmOrderImg res = hmOrderImgService.getById(id);
            if (res != null) {
                return ResultUtil.data(res, "查询成功");
            } else {
                return ResultUtil.data(res, "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：实现分页查询
     *
     * @param searchVo 需要模糊查询的信息
     * @param pageVo   分页参数
     * @return 返回获取结果
     */
    @ApiOperation("分页查询订单技师上传的图片数据")
    @GetMapping("queryHmOrderImgList")
    public Result<Object> queryHmOrderImgList(HmOrderImg hmOrderImg, SearchVo searchVo, PageVo pageVo) {
        try {
            IPage<HmOrderImg> result = hmOrderImgService.queryHmOrderImgListByPage(hmOrderImg, searchVo, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：导出数据
     *
     * @param response   请求参数
     * @param hmOrderImg 查询参数
     * @return
     */
    @ApiOperation("导出订单技师上传的图片数据")
    @PostMapping("/download")
    public void download(HttpServletResponse response, HmOrderImg hmOrderImg) {
        try {
            hmOrderImgService.download(hmOrderImg, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
